class: title-slide # ER018 - Analyzing Business Relations & Documents ## PVA3 ### Teil 2: Analyse Räumlicher Daten in R <br> <br> <br> <br> <br> <br> <br> ### FS 2024 <br> ### Prof. Dr. Jörg Schoder .mycontacts[
@FFHS-EconomicResearch
@jfschoder ] --- layout: true <div class="my-footer"></div> <div style="position: absolute;left:400px;bottom:10px;font-size:9px">
Prof. Dr. Jörg Schoder</div> --- name: agenda class: left .blockquote[Agenda] ## Analyse Räumlicher Daten in R * Karten erstellen * Räumliche Regression * Räumliche Netzwerke ??? [Beispiel](https://crd230.github.io/lab9.html) [Scott](https://www.esri.com/news/arcwatch/0410/lauren-scott.html) --- class: inverse, center, middle ## Kartenerstellung in R .blockquote[Einfache Kartierung mit ggplot] .blockquote[simple features und das sf-Paket] .blockquote[Interaktive Karten mit Leaflet] .blockquote[Schweizer Geodaten] --- class: left .blockquote[Einfache Kartierung mit ggplot] ## Simple Karte .panelset[ .panel[.panel-name[Top5 CH-Städte] ```r library(tidyverse) my_in_file <- "CitiesCH_(simplemaps.com).csv" tbl_citiesCH <- read_csv(xfun::from_root("data","raw","PVA3",my_in_file)) tbl_citiesCH5 <- tbl_citiesCH %>% arrange(desc(population)) %>% slice_head(n=5) tbl_citiesCH5 ``` ``` ## # A tibble: 5 × 9 ## city lat lng country iso2 admin_name capital population ## <chr> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl> ## 1 Zürich 47.4 8.54 Switzerland CH Zürich admin 447082 ## 2 Geneva 46.2 6.15 Switzerland CH Genève admin 203840 ## 3 Basel 47.6 7.59 Switzerland CH Basel-Stadt admin 173552 ## 4 Lausanne 46.5 6.63 Switzerland CH Vaud admin 141418 ## 5 Bern 46.9 7.45 Switzerland CH Bern primary 134506 ## # ℹ 1 more variable: population_proper <dbl> ``` ] .panel[.panel-name[Einfache Abbildung] ```r tbl_citiesCH5 %>% ggplot(aes(x = lng, y = lat)) + geom_point() ``` <img src="data:image/png;base64,#02_SpatialStats_in_R_files/figure-html/unnamed-chunk-2-1.png" width="50%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Labels ergänzen] ```r tbl_citiesCH5 %>% ggplot(aes(x = lng, y = lat)) + geom_point() + geom_text(aes(label=city),hjust=1, vjust=-0.5) + scale_x_continuous(limits = c(5.5,9)) ``` <img src="data:image/png;base64,#02_SpatialStats_in_R_files/figure-html/unnamed-chunk-3-1.png" width="50%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Gewichtung] ```r tbl_citiesCH5 %>% ggplot(aes(x = lng, y = lat)) + geom_point(aes(size=population)) + geom_text(aes(label=city),hjust=1, vjust=-0.5) + scale_x_continuous(limits = c(5.5,9)) ``` <img src="data:image/png;base64,#02_SpatialStats_in_R_files/figure-html/unnamed-chunk-4-1.png" width="50%" style="display: block; margin: auto;" /> ] ] --- class: left .blockquote[Einfache Kartierung mit ggplot] ## Arten räumlicher Daten <iframe src="https://www.paulamoraga.com/book-spatial/types-of-spatial-data.html" width="100%" height="480px" data-external="1"></iframe> ??? * Flächenbezogene Daten (bspw. Kanton und Bevölkerungsanzahl) * Geodaten (mit Koordinaten) - bekannte Orte * Punktmuster (mit Koordinaten) - unbekannte/zufällige Orte, bspw´. Waldbrand * Raum-Zeit-Daten. Entwicklung über die Zeit, bspw. Luftverschmutzung, Kaufkraft... * Mobilitätsdaten --- class: left .blockquote[simple features und das sf-Paket] ## Dateiformate für räumliche Daten <br> .pull-left[ <img src="data:image/png;base64,#../../img/PVA3/Vektordaten_Dateiformate_(SurveyGyaan_2022)_medium.PNG" width="100%" style="display: block; margin: auto;" /> ] -- .pull-right[ <img src="data:image/png;base64,#../../img/PVA3/Rasterdaten_Dateiformate_(SurveyGyaan_2022)_medium.PNG" width="100%" style="display: block; margin: auto;" /> ] .quelle[Quelle: <a name=cite-surveygyaan_vector_2022></a>[Surveygyaan (2022b)](#bib-surveygyaan_vector_2022), <a name=cite-surveygyaan_raster_2022></a>[Surveygyaan (2022a)](#bib-surveygyaan_raster_2022).] --- class: left .blockquote[simple features und das sf-Paket] ## Warum das sf-Paket? * Schnelles Lesen und Schreiben von Daten -- * Verbesserte Leistung beim Plotten -- * Verwendung von `sf`-Objekten (in der Regel) wie `data.frame`- oder `tibble-Objekte` -- * konsistente und intuitive Verben als Funktionsnamen (beginnen mit `st_`) -- * Kompatibilität mit tidyverse-Synatax und Verwendung mit Pipes --- class: left .blockquote[simple features und das sf-Paket] ## Simple Features und sf-Paket <img src="data:image/png;base64,#../../img/PVA3/02-sfdiagram.png" width="80%" style="display: block; margin: auto;" /> Elemente von sf-Objekten: * sfg * sfc --- class: left .blockquote[Kartenerstellung] ## Kartenerstellung mit `geom_sf()` in ggplot2 <iframe src="https://www.paulamoraga.com/book-spatial/making-maps-with-r.html" width="100%" height="480px" data-external="1"></iframe> --- class: left ## Kartogramme als alternative Darstellungsform <img src="data:image/png;base64,#../../img/PVA3/Grid_People_Population_2020_(worldmapper.org).png" width="100%" style="display: block; margin: auto;" /> .quellePan[Bildquelle: [worldmapper.org](https://worldmapper.org/maps/grid-population-2020/).] --- class: left .blockquote[Schweizer Geodaten] ## Landesgrenze Schweiz ```r library(ggswissmaps) ggswissmaps::maps2$g1l15 ``` <!-- --> --- class: left .blockquote[Schweizer Geodaten] ## Aufgabe 1. Verwende das **ggswissmaps**-Paket und suche aus dem Objekt `maps2` den Plot der Schweiz, der neben der Landesgrenze auch die Kantone abbildet. 2. Verwende das **ggswissmaps**-Paket und suche aus dem Objekt `maps2` den Plot der die Gewässer abbildet. -- ```r ggswissmaps::maps2$g1k15 ``` ```r ggswissmaps::maps2$g1s15 ``` --- class: left .blockquote[Schweizer Geodaten] ## SBB-Liniennetz <!-- --> ??? [swissgd](https://github.com/zumbov2/swissgd) [bfsMaps](https://cran.r-project.org/web/packages/bfsMaps/bfsMaps.pdf) [RSwissMaps](https://github.com/zumbov2/RSwissMaps) aktuell noch nicht verfügbar für R4.4.0 --- class: left ```r library(geojsonsf) #sf <- geojson_sf("https://data.sbb.ch/api/explore/v2.1/catalog/datasets/linie-mit-polygon/exports/geojson") sf_london <- geojson_sf(xfun::from_root("data","raw","PVA3","london_421.geojson")) sf_zh <- geojson_sf(xfun::from_root("data","raw","PVA3","zurich-small_.geojson")) p_zh <- sf_zh %>% ggplot2::ggplot() + geom_sf() + theme_void() p_ld <- sf_london %>% ggplot2::ggplot() + geom_sf() + theme_void() library(patchwork) p_zh+p_ld ``` <!-- --> ??? Quelle: [](https://cartographyvectors.com/map/421-london#google_vignette) --- class: left ## Aufgabe * Suche eine geojson-Datei mit Geoinformationen zu Zürich und speichere die .geojson-Datei im Ordner "data/raw" * Lade die Datei als sf-Objekt * Stelle die Umrisse der Stadt Zürich mit Hilfe des `geom_sf()` aus dem **ggplot2**-Paket ??? ```r library(geojsonsf) #sf <- geojson_sf("https://data.sbb.ch/api/explore/v2.1/catalog/datasets/linie-mit-polygon/exports/geojson") sf_london <- geojson_sf(xfun::from_root("data","raw","PVA3","london_421.geojson")) sf_zh <- geojson_sf(xfun::from_root("data","raw","PVA3","zurich-small_.geojson")) p_zh <- sf_zh %>% ggplot2::ggplot() + geom_sf() + theme_void() p_ld <- sf_london %>% ggplot2::ggplot() + geom_sf() + theme_void() library(patchwork) p_zh+p_ld ``` <!-- --> Quelle: [](https://cartographyvectors.com/map/421-london#google_vignette) --- class: left ## Airbnb Zürich .panelset[ .panel[.panel-name[Bezirke] ```r library(leaflet) ZH_locations$geometry %>% leaflet() %>% addTiles() %>% addPolygons(label = paste0("Kreis ", tidyr::extract_numeric(ZH_locations$neighbourhood_group), ", ", ZH_locations$neighbourhood)) ``` ``` ## extract_numeric() is deprecated: please use readr::parse_number() instead ```
] .panel[.panel-name[Angebote] ```r ZH_listings %>% select(latitude,longitude) %>% leaflet() %>% addTiles() %>% addMarkers() ```
] ] ??? ```r library(tidyverse) tbl_starbucks <- read_csv(xfun::from_root("data","raw","PVA3","starbucks_(kaggle).zip")) starbucks_zh <- tbl_starbucks %>% filter(City=="Zurich"&Country=="CH") ``` --- class: left ## Permutation <img src="data:image/png;base64,#../../img/PVA3/worldmap_permutation_(Jacobs_2010)_ritzholtz.com.jpg" width="90%" style="display: block; margin: auto;" /> .quelle[Bildquelle: [ritholtz.com](https://ritholtz.com/2010/11/world-map-rearranged-by-population/).] --- class: inverse,center,middle # Vielen Dank für die Aufmerksamkeit! --- background-image: url("data:image/png;base64,#http://bit.ly/cs631-donkey") background-size: 80% --- class: left ## Quellenverzeichnis .ref-slide[ <a name=bib-surveygyaan_raster_2022></a>[Surveygyaan](#cite-surveygyaan_raster_2022) (2022a). _Raster Data in GIS_. (Visited on Mai. 21, 2024). <a name=bib-surveygyaan_vector_2022></a>[Surveygyaan](#cite-surveygyaan_vector_2022) (2022b). _Vector Data in GIS_. (Visited on Mai. 21, 2024). ]